package com.beetlestech.nfe.main.biz.com.impl;

import java.util.List;

import java.util.ArrayList;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.main.biz.com.HbVAdminList;
import com.beetlestech.nfe.main.biz.com.dto.VadmininfolistInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.view.VadmininfolistDTO;
import com.beetlestech.nfe.main.db.view.VadmininfolistbyusernameDTO;
public class HbVAdminListImpl extends AbstractDBLogic implements HbVAdminList, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	
	@Override
	public VadmininfolistInfoDTO[] getAdminList(String userName, Integer jurisdictionid) {
		List<VadmininfolistInfoDTO> adminList = new ArrayList<VadmininfolistInfoDTO>();
		VadmininfolistDTO vadmininfolistDTO =new VadmininfolistDTO();
		if((userName==null ||userName.equals("")) && jurisdictionid!=0){
			vadmininfolistDTO.setjurisdictionid(jurisdictionid);
			List<VadmininfolistDTO> adminListDb=queryDAO.queryForObjectList(VadmininfolistDTO.EQUAL_SEARCH, vadmininfolistDTO);
			if(adminListDb==null){
				createApplicationException("errMsg", "没找到要查询的信息");
			}else{
				for(VadmininfolistDTO item:adminListDb){
					adminList.add(convertToVAdminListInfo(item));
				}
			}
		}
		else if((userName!=null && !userName.equals(""))&& jurisdictionid==0){
			StatementDTO statementDTO = new StatementDTO();
			StatementUtil.andWhere(statementDTO, VadmininfolistbyusernameDTO.COLUMN_USERNAME, StatementUtil.KEYWORD.LIKE ,userName);
			VadmininfolistbyusernameDTO searchDTO = new VadmininfolistbyusernameDTO();
			searchDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
			List<VadmininfolistbyusernameDTO> adminListByLikeName =
					queryDAO.queryForObjectList(VadmininfolistbyusernameDTO.SQL_SEARCH, searchDTO);
			if(adminListByLikeName==null){
				createApplicationException("errMsg", "没找到要查询的信息");
			}else{
				for(VadmininfolistbyusernameDTO item:adminListByLikeName){
					adminList.add(convertToVadmininfolistbyusername(item));
				}
			}
		}        
		else if((userName!=null && !userName.equals(""))&&jurisdictionid!=0){
			vadmininfolistDTO.setjurisdictionid(jurisdictionid);
			vadmininfolistDTO.setusername(userName);
			StatementDTO statementDTO = new StatementDTO();
			StatementUtil.andWhere(statementDTO, VadmininfolistDTO.COLUMN_JURISDICTION_ID, StatementUtil.KEYWORD.EQUAL ,jurisdictionid);
			StatementUtil.andWhere(statementDTO, VadmininfolistDTO.COLUMN_USERNAME, StatementUtil.KEYWORD.LIKE ,userName);
			vadmininfolistDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
			List<VadmininfolistDTO> adminListDbT=queryDAO.queryForObjectList(VadmininfolistDTO.SQL_SEARCH, vadmininfolistDTO);
			if(adminListDbT==null){
				createApplicationException("errMsg", "没找到要查询的信息");
			}else{
				for(VadmininfolistDTO item:adminListDbT){
					adminList.add(convertToVAdminListInfo(item));
				}
			}
			
		}else if((userName==null ||userName.equals(""))&&jurisdictionid==0){
			VadmininfolistbyusernameDTO searchDTO = new VadmininfolistbyusernameDTO();
			List<VadmininfolistbyusernameDTO> adminListByLikeName =
					queryDAO.queryForObjectList(VadmininfolistbyusernameDTO.SQL_SEARCH,searchDTO);	
			if(adminListByLikeName==null){
				createApplicationException("errMsg", "没找到要查询的信息");
			}else{
				for(VadmininfolistbyusernameDTO item:adminListByLikeName){
					adminList.add(convertToVadmininfolistbyusername(item));
				}
			}
			
		}
		return adminList.toArray(new VadmininfolistInfoDTO[adminList.size()]);
	}
	private VadmininfolistInfoDTO convertToVAdminListInfo(VadmininfolistDTO item){
		VadmininfolistInfoDTO vadmininfolistInfoDTO = new VadmininfolistInfoDTO();
		vadmininfolistInfoDTO.setUsrId(item.getusrid());
		vadmininfolistInfoDTO.setUserName(item.getusername());
		vadmininfolistInfoDTO.setJurisdictionId(item.getjurisdictionid());
		vadmininfolistInfoDTO.setCreateDate(item.getcreatedate());
		return vadmininfolistInfoDTO;
	}
	
	private VadmininfolistInfoDTO convertToVadmininfolistbyusername(VadmininfolistbyusernameDTO item){
		VadmininfolistInfoDTO vadmininfolistInfoDTO = new VadmininfolistInfoDTO();
		vadmininfolistInfoDTO.setUsrId(item.getusrid());
		vadmininfolistInfoDTO.setUserName(item.getusername());
		vadmininfolistInfoDTO.setJurisdictionId(item.getjurisdictionid());
		vadmininfolistInfoDTO.setCreateDate(item.getcreatedate());
		return vadmininfolistInfoDTO;
	}
}
